<template>

  <el-dialog
    :visible.sync="ShopConfigDialogVisible"
    title="配置门店"
  >
    <div class="tenantManagement-container">

      <el-collapse-transition>
        <div v-show="this.moreQueryFlag" class="more-query">
          <!-- 更多查找 -->
          <vab-query-form>
            <vab-query-form-left-panel :span="24">
              <el-form :inline="true" :model="queryForm" @submit.native.prevent>


              </el-form>
            </vab-query-form-left-panel>

          </vab-query-form>
          <el-divider></el-divider>
        </div>
      </el-collapse-transition>

      <!-- 主要操作  -->
      <vab-query-form>
        <vab-query-form-left-panel :span="10">
          <el-button
            v-if="$perms('shop_shopconfig_insert')"
            icon="el-icon-plus"
            type="primary"
            @click="handleInsert"
          > 添加
          </el-button>

        </vab-query-form-left-panel>
        <vab-query-form-right-panel :span="14">
          <el-form :inline="true" :model="queryForm" @submit.native.prevent>


            <el-form-item>
              <el-input
                v-model.trim="queryForm.value_LIKE"
                clearable
                placeholder="请输入值"
              />
            </el-form-item>

            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="queryData">
                查询
              </el-button>


            </el-form-item>
          </el-form>
        </vab-query-form-right-panel>
      </vab-query-form>

      <el-table
        v-loading="listLoading"
        :data="list"
        :element-loading-text="elementLoadingText"
        @selection-change="setSelectRows"
      >
        <el-table-column
          label="默认"
          prop="isDefault"
          show-overflow-tooltip
          width="60"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.isDefault == 1 ? "是" : "否" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="名称"
          prop="name"
          show-overflow-tooltip
          width="100"
        ></el-table-column>

        <el-table-column
          label="值"
          prop="value"
          show-overflow-tooltip
        >
          <template v-slot="scope">
            <div v-if="scope.row.type === 1">
              <div v-if="scope.row.value" style="display: flex">
                <el-image v-for="item in scope.row.value.split(',')" :key="item"
                          :preview-src-list="scope.row.value.split(',')"
                          :src="item"
                          style="width: 50px;margin-left: 5px">
                </el-image>
              </div>

            </div>
            <div v-else-if="scope.row.type === 5">
              <div v-if="scope.row.value" style="display: flex">
                <el-tag
                  v-for="tag in scope.row.value.split(',')"
                  :key="tag"
                >
                  {{ tag }}
                </el-tag>
              </div>

            </div>


            <div v-else>
              {{ scope.row.value }}
            </div>
          </template>

        </el-table-column>

        <el-table-column
          label="备注"
          prop="remarks"
          show-overflow-tooltip
        ></el-table-column>

        <el-table-column
          v-if="$perms('shop_shopconfig_update') || $perms('shop_shopconfig_delete')"
          label="操作"
          show-overflow-tooltip
          width="100"
        >
          <template v-slot="scope">
            <el-button
              v-if="$perms('shop_shopconfig_update')"
              type="text"
              @click="handleUpdate(scope.row)"
            > 编辑
            </el-button>

            <el-divider direction="vertical"></el-divider>

            <el-button
              v-if="$perms('shop_shopconfig_delete')"
              :disabled="scope.row.isDefault === 1"
              type="text"
              @click="handleDelete(scope.row)"
            > 删除
            </el-button>
          </template>

        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="queryForm.pageNo"
        :layout="layout"
        :page-size="queryForm.pageSize"
        :total="total"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>

      <edit ref="edit" @fetchData="fetchData"></edit>
      <import ref="import" @fetchData="fetchData"></import>

    </div>
  </el-dialog>

</template>

<script>
import {doDelete, doDeleteAll, doExportExcel, getList} from "@/api/shop/shopconfig/ShopConfigManagementApi";
import Edit from "@/views/modules/shop/shopconfig/components/ShopConfigManagementEdit.vue";
import Import from "@/views/modules/shop/shopconfig/components/ShopConfigManagementImport.vue";

import {vueButtonClickBan} from "@/utils";
import {isNotNull} from "@/utils/valiargs";

export default {
  name: "ShopConfigManagement",
  components: {Edit, Import},
  props: {
    QorgIds: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      ShopConfigDialogVisible: false,
      list: null,
      listLoading: true,
      layout: "total, prev, pager, next, sizes, jumper",
      total: 0,
      selectRows: "",
      elementLoadingText: "正在加载...",
      moreQueryFlag: false,
      queryForm: {
        pageNo: 1,
        pageSize: 10,
        name_LIKE: "",
        value_LIKE: "",
        orgIds_LIKE: null,
      },
      dict: {},
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
    };
  },
  created() {
    
  },
  mounted() {
  },
  methods: {
    show(id) {
      this.ShopConfigDialogVisible = true
      this.queryForm.orgIds_LIKE = id;
      this.fetchData();
    },
    setSelectRows(val) {
      this.selectRows = val;
    },
    handleInsert(row) {
      this.$refs["edit"].showEdit();
    },
    handleUpdate(row) {
      if (row.id) {
        this.$refs["edit"].showEdit(row);
      }
    },
    handleDelete(row) {
      if (row.id) {
        this.$baseConfirm("你确定要删除当前项吗", null, async () => {
          const {msg} = await doDelete({id: row.id});
          this.$baseMessage(msg, "success");
          await this.fetchData();
        });
      } else {
        if (this.selectRows.length > 0) {
          const ids = this.selectRows.map((item) => item.id).join();
          this.$baseConfirm("你确定要删除选中项吗", null, async () => {
            const {msg} = await doDeleteAll({ids});
            this.$baseMessage(msg, "success");
            await this.fetchData();
          });
        } else {
          this.$baseMessage("未选中任何行", "error");
          return false;
        }
      }
    },
    // 导出excel
    handleExportExcel(el) {
      // 导出按钮防抖处理 默认限制为10秒
      vueButtonClickBan(el, 10);

      // 执行导出
      doExportExcel(this.queryForm);
    },
    // 导入excel
    handleImportExcel() {
      this.$refs["import"].show();
    },


    handleSizeChange(val) {
      this.queryForm.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.pageNo = val;
      this.fetchData();
    },
    moreQuery() {
      this.moreQueryFlag = !this.moreQueryFlag;
    },
    queryData() {


      this.queryForm.pageNo = 1;
      this.fetchData();
    },
    async fetchData() {
      this.listLoading = true;
      const {data} = await getList(this.queryForm);
      if (isNotNull(data)) {
        this.list = data.rows;
        this.total = data.total;
      }
      setTimeout(() => {
        this.listLoading = false;
      }, 300);
    },
  },
};
</script>
